package com.programmercarl.backtracking;

import java.util.ArrayList;
import java.util.List;

/**
 * https://leetcode.cn/problems/subsets/
 * 集合
 *
 * @author yulongtian
 * @create 2025-01-11 10:56
 */
public class Test08 {
    public static void main(String[] args) {
        Test08 test08 = new Test08();
        System.out.println(test08.subsets(new int[]{1, 2, 3}));
    }

    public List<List<Integer>> subsets(int[] nums) {
        List<List<Integer>> ans = new ArrayList<List<Integer>>();
        ans.add(new ArrayList<>(0));
        List<Integer> path = new ArrayList<>();

        backtrack(nums, 0, ans, path);

        return ans;
    }

    public void backtrack(int[] nums, int index, List<List<Integer>> ans, List<Integer> path) {
        if (index == nums.length) {
            return;
        }
        for (int i = index; i < nums.length; i++) {
            path.add(nums[i]);
            ans.add(new ArrayList<>(path));
            backtrack(nums, i + 1, ans, path);
            path.remove(path.size() - 1);
        }
    }

}
